<template>
    <div id="disk">
		
		<div style="width: 100%;height: 50px;padding: 10px">
			<Button type="primary" @click="modal1 = true" style="float: right;">挂载SMB磁盘</Button>
			<h2 style="text-align: center;padding-left: 180px;">管理SMB挂载磁盘</h2>
		</div>
		
       
    <Modal
        v-model="modal1"
        title="SMB连接"
        @on-ok="ok"
        @on-cancel="cancel">
      <div >
          <div style="wight:100%;">
            <Checkbox v-model="single" >是否有密码</Checkbox>
          </div>
          
           <Input v-model="smbpath" placeholder="SMB共享路径:\\xxx.xxx.xxx.xxx\xxx" style="width: 300px" />
           <Input v-show="single" v-model="username" placeholder="SMB共享账号" style="width: 300px"></Input>
           <Input v-show="single" v-model="password" placeholder="SMB共享密码" style="width: 300px"></Input>
      </div>
    </Modal>

    <ul v-for="(item,index) in arr" class="ullist" @contextmenu="showMenu(index)">
        <vue-context-menu :contextMenuData="contextMenuData" @deleted="deleted" @NewFile="NewFile"></vue-context-menu>
       <div style="padding:20px" @dblclick="dbclick(index)">
            <img src="/src/renderer/assets/icon/6.png" style="width:70px;height:70px;float:left;">
            <div style="margin-top:10px;user-select: none;">
                <h2 >{{item.smbip}}</h2>
                <h4 >文件夹名称：{{item.name}}</h4>
            </div>
       </div>
       </ul>
    </div>
</template>
<script>
import { dirname } from 'path';
var exec = require("child_process");
var fs = require('fs')
export default {
    data(){
        return{
            single:false,
            arr:[],
            list:[],
            modal1: false,
            smbpath:'',
            username:'',
            password:'',
            contextIndex:'',
            contextMenuData: {
					menuName: 'demo',
					//菜单显示的位置
					axis: {
						x: null,
						y: null
					},
					//菜单选项
					menulists: [{
						fnHandler: 'deleted', //绑定事件
						btnName: '删除', //菜单名称
					}, {
						fnHandler: 'NewFile',
						btnName: '重命名'
					}]
				},
        }
    },
    mounted(){
        console.log(this.smbpath)
         // 读取配置文件 已经保存的SMB磁盘
            fs.readFile('./SMBconfig.ini', 'utf-8', (err, data)=> {
                if (err) {
                    throw err;
                }
                // 读取配置文件 并转换成json数组
               var arrjson = eval(data)
                this.arr = arrjson
                console.log(this.arr)
            });
    },
    methods:{
        ok () {

            var obj = []
           var arr = this.arr
           var smbpath = this.smbpath
           console.log(smbpath)
            var username  = this.username
           var password  = this.password
          
        var dirName = smbpath.substring(smbpath.lastIndexOf('\\')+1)
        var dirPath = smbpath.substring(smbpath.indexOf('\\')+2,smbpath.lastIndexOf('\\'))
        console.log(dirPath)
        console.log('文件夹名称：'+dirName)
         // 创建SMB文件夹
            fs.mkdir('/home/smb/', function(err,data) {
                if (err) {
                    throw err;
                }
                console.log('创建文件夹成功！')
            });

        // 创建文件夹
            fs.mkdir('/home/smb/' + dirName, function(err,data) {
                if (err) {
                    throw err;
                }
                console.log('创建文件夹成功！')
            });

        if(this.username != ''){
           
            // sudo umount /home/smb/迅雷下载
                var cmd = 'sudo mount -t cifs -o username='+this.username+',password='+this.password+' \\\\\\\\'+dirPath+'\\\\'+dirName+' /home/smb/' + dirName
                exec.exec(cmd, function(error, stdout, stderr) {
                    if(error){
                    console.error(error);
                    alert('挂载失败，请检查SMB共享地址是否正确！')
                    return;
                }
                else{
                   alert('挂载成功，已经与SMB共享地址连接！')
                }
                });
        }else{
            
                 var cmd = 'sudo mount -t cifs -o username=1,password=1, \\\\\\\\'+dirPath+'\\\\'+dirName+' /home/smb/' + dirName
                exec.exec(cmd, function(error, stdout, stderr) {
                    if(error){
                    console.error(error);
                    alert('挂载失败，请检查SMB共享地址是否正确！')
                    return;
                }
                else{
                    console.log('挂载SMB成功');
                    alert('挂载成功，已经与SMB共享地址连接！')
                }
                });
        }


        // 利用FS模块 获取NFS目录信息
        var readDir = fs.readdirSync('/home/smb/'+dirName,'utf-8');
        console.log(readDir)
        //将该目录渲染到list数组里面
        this.list = readDir

        obj = [{
               'name':dirName,
               'smbip':dirPath,
               'path':smbpath
           }]

          this.arr =  arr.concat(obj)
            console.log(this.arr)

            // 将NFS数组转换成字符串
            // 写入test.ini 配置文件中
            console.log(JSON.stringify(this.arr))
            fs.writeFile('./SMBconfig.ini', JSON.stringify(this.arr), (err)=> {
                if (err) {
                    throw err;
                }
                console.log('写入成功');
            });

            // 写入成功后读取测试
            fs.readFile('./SMBconfig.ini', 'utf-8', (err, data)=> {
                if (err) {
                    throw err;
                }
                // 读取配置文件 并转换成json数组
               var arrjson = eval(data)
               console.log(arrjson)
            });
            },

            cancel () {
                this.$Message.info('取消连接');
            },
            // 双击标签事件
            dbclick(index){
               var smb = this.arr[index]
               console.log(JSON.stringify(smb))
               window.sessionStorage.setItem('SMB_Path',JSON.stringify(smb))
               this.$router.push('/SMB_Home')
               
            },
            // 右键菜单
            showMenu(index) {
				event.preventDefault()
				var x = event.clientX
				var y = event.clientY
				// Get the current location
				this.contextMenuData.axis = {
					x,
					y
				}
				this.contextIndex = index
            },
            deleted(){
                const index = this.contextIndex
                console.log(index)

            this.arr.splice(index,index+1);
            console.log(this.arr)

            // 将NFS数组转换成字符串
            // 写入test.ini 配置文件中
            console.log(JSON.stringify(this.arr))
            fs.writeFile('./SMBconfig.ini', JSON.stringify(this.arr), (err)=> {
                if (err) {
                    throw err;
                }
                console.log('写入成功');
            });

            // 写入成功后读取测试
            fs.readFile('./SMBconfig.ini', 'utf-8', (err, data)=> {
                if (err) {
                    throw err;
                }
                // 读取配置文件 并转换成json数组
               var arrjson = eval(data)
               console.log(arrjson)
            });
            }
    }
}
</script>
<style >
.ullist {
		text-align: center;
		list-style: none;
		width: 300px;
		overflow: hidden;
		text-overflow:ellipsis;
		white-space: nowrap;
	}
	
	.ullist:hover{
	background-color: #e5f3ff
	} 
	
	
</style>